﻿/*
 * This file is part of MonoSettlers.
 *
 * Copyright (C) 2010-2011 Christoph Husse
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: 
 *      # Christoph Husse
 * 
 * Also checkout our homepage: http://opensettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using OpenTK;
using OpenTK.Input;
using OpenTK.Graphics;
using System.Runtime.InteropServices;

#if EMBEDDED
    using OpenTK.Graphics.ES20;
#else
using OpenTK.Graphics.OpenGL;
#endif

namespace MonoSettlers.RenderSystem
{
    public partial class GLRenderer
    {
        public void RaiseMouseMove(int inNewMouseX, int inNewMouseY)
        {
            inNewMouseX = Math.Max(0, Math.Min(inNewMouseX, ViewportWidth - 1));
            inNewMouseY = Math.Max(0, Math.Min(inNewMouseY, ViewportWidth - 1));

            MouseXY = new Point(inNewMouseX, inNewMouseY);
        }

        public void RaiseMouseDown(int inMouseButton)
        {
            if (OnMouseDown != null)
                OnMouseDown(this, inMouseButton);
        }

        public void RaiseMouseUp(int inMouseButton)
        {
            if (OnMouseUp != null)
                OnMouseUp(this, inMouseButton);
        }

        void Keyboard_KeyUp(object sender, KeyboardKeyEventArgs e)
        {
            int btn = (int)e.Key;

            lock (m_KeyboardState)
            {
                m_KeyboardState.Remove(btn);
            }

            if (OnKeyUp != null)
                OnKeyUp(this, btn);
        }

        void Keyboard_KeyDown(object sender, KeyboardKeyEventArgs e)
        {
            int btn = (int)e.Key;

            lock (m_KeyboardState)
            {
                m_KeyboardState.Add(btn);
            }

            if (OnKeyDown != null)
                OnKeyDown(this, btn);
        }

        void UpdateKeyboardState()
        {
            if ((m_KeyboardState.Count > 0) && (OnKeyRepeat != null))
                OnKeyRepeat(this);
        }

        void UpdateMouseState()
        {
            //MouseXY = new Point(m_Window.Mouse.X, m_Window.Mouse.Y);

            if ((m_MouseState.Count > 0) && (OnMouseDrag != null))
                OnMouseDrag(this);
        }

        void Mouse_ButtonUp(object sender, MouseButtonEventArgs e)
        {
            int btn = (int)e.Button;

            UpdateMouseState();

            lock (m_MouseState)
            {
                m_MouseState.Remove(btn);
            }

            if (OnMouseUp != null)
                OnMouseUp(this, btn);
        }

        void Mouse_ButtonDown(object sender, MouseButtonEventArgs e)
        {
            int btn = (int)e.Button;

            UpdateMouseState();

            lock (m_MouseState)
            {
                m_MouseState.Add(btn);
            }

            if (OnMouseDown != null)
                OnMouseDown(this, btn);
        }
    }
}
